package net.huashitong.scheduling.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.StringUtils;
import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.scheduling.entity.Project;
import net.huashitong.scheduling.filter.ProjectFilter;

@Repository
public class ProjectDao extends StringDao<Project> {
    public List<Project> queryByConstructionAddress(ProjectFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.date = :date").setParam("date", filter.getDate());
        query.append(" and obj.constructionAddress like :constructionAddress").setParam("constructionAddress", "%" + filter.getName() + "%");
        return query.listResult();
    }

    public List<Project> queryByType(ProjectFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.date = :date").setParam("date", filter.getDate());
        query.append(" and obj.type = :type order by cast(ratio*100 as int) DESC").setParam("type", filter.getNid());
        return query.listResult();
    }

    public List<Project> queryByLinkedLeadership(ProjectFilter filter) {
        String currentYear;
        if(StringUtils.isBlank(filter.getDate())){
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy");
            currentYear = sf.format(date);
        }else{
            currentYear = filter.getDate();
        }
        GenericQuery query = createQuery("obj");
        if (ObjectUtils.isNotBlank(filter.getName())) {
            query.append(" and obj.linkedLeadership like :linkedLeadership GROUP BY obj.linkedLeadership ").setParam("linkedLeadership", "%" + filter.getName() + "%");
            query.append(" and obj.date like :date ").setParam("date", "%" + currentYear + "%");

        } else {
            query.append(" and obj.date like :date ").setParam("date", "%" + currentYear + "%");
            query.append(" GROUP BY obj.linkedLeadership ");
        }

        return query.listResult();
    }

    /**
     * 获取项目最新时间
     * @return
     */
    public String getLastProjectDate(){
        GenericQuery query = createQuery("obj");
        query.setOrder("date+0", BaseFilter.DESC);
        Project project = query.firstResult();
        return project.getDate();
    }

    //搜索2019年项目
    public List<Project> queryByProjectName(ProjectFilter filter) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String currentYear = sf.format(date);
        GenericQuery query = createQuery("obj");
        query.append(" and ( obj.projectName like :projectName or obj.linkedLeadership like :linkedLeadership )").setParam(
                "projectName", "%" + filter.getName() + "%").setParam("linkedLeadership", "%" + filter.getName() + "%");
        query.append(" and obj.date like :date").setParam("date", "%" + currentYear + "%");
        return query.listResult();

    }

    public List<Project> queryByLinkedLeadershipName(ProjectFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.linkedLeadership like:linkedLeadership  ").setParam("linkedLeadership", "%" + filter.getName() + "%");
        query.append(" and obj.date like :date").setParam("date", "%" + "2019" + "%");
        return query.listResult();
    }
}
